package com.bdqn.service.impl;

import com.bdqn.entity.responseDate.cart.OmsCartDto;
import com.bdqn.entity.responseDate.pmsProduct.PmsSkuDto;
import com.bdqn.entity.util.Constants;
import com.bdqn.entity.util.ResultEnum;
import com.bdqn.mapper.PmsSkuMapper;
import com.bdqn.service.OmsCartService;
import com.bdqn.util.BaseException;
import com.bdqn.util.Pet_service_RedisTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 对购物车进行相关的增删该改查操作
 */
@Service
public class OmsCartServiceImpl implements OmsCartService {
    /**
     * 对购物车进行添加购物信息操作
     */
    @Autowired
    private Pet_service_RedisTemplate pet_service_redisTemplate;
    @Autowired
    private PmsSkuMapper pmsSkuMapper;

    @Override
    public int add(Long skuId, Integer num, Long userId) {
        //通过前台传来的skuid查询出来对应的sku信息
        PmsSkuDto pmsSkuDto = pmsSkuMapper.pmsSkuDto(skuId);
//        if(pmsSkuDto==null){
//            return 666;
//        }
        //先通过key和hashKey去查询对象是否存在
        OmsCartDto cart = pet_service_redisTemplate.getCart(userId, pmsSkuDto);
        System.out.println("从redis中获取的购物对象为"+cart);
        if (cart == null) {
            //校验商品信息是否合法
            checkProduct(pmsSkuDto,num);
            //添加新的购物信息
            OmsCartDto omsCartDto = new OmsCartDto(pmsSkuDto, num, userId);
          return   pet_service_redisTemplate.addCart(userId, pmsSkuDto, omsCartDto);
        } else {
            //表示购物信息存在 那么更新购物信息的个数
            num+=cart.getNum();
            cart.setNum(num);
          return   pet_service_redisTemplate.addCart(userId,pmsSkuDto,cart);
        }

    }

    @Override
    public int remove(Long userId,List<Long> skuIds) {
        return pet_service_redisTemplate.remove(userId, skuIds);
    }

    //校验商品信息
    Logger logger = LoggerFactory.getLogger(OmsCartServiceImpl.class);

    private void checkProduct(PmsSkuDto pmsSkuDto, int num) {
        if (num <=0) {
            logger.info("向购物车中加入num:{}件商品", num);
            throw new BaseException(ResultEnum.ADD_CART_MIN_NUM_ERROR);
        }
        if (num > pmsSkuDto.getStock()) {
            logger.info("加入购物车的商品skuId：{}，数量为：{}，超过了最大库存数量：{}"
                    , pmsSkuDto.getId(), num, pmsSkuDto.getStock());
            throw new BaseException(ResultEnum.ADD_CART_LOW_STOCK_ERROR);
        }
        // 商品上下架等状态判断
        if (pmsSkuDto.getPublishStatus().equals(Constants.Spu.PUBLISH_STATUS_OFF)) {
            logger.info("要加入购物车的商品skuId：{}已下架", pmsSkuDto.getId());
            throw new BaseException(ResultEnum.PRODUCT_STATUS_OFF);
        }
    }



    /**
     * 获取购物车的长度  通过判断获取的map集合的长度
     * @param userId
     * @return
     */
    @Override
    public int cartCount(Long userId) {
        //获取key对应的map对象
        Map<Object, Object> hashValues = pet_service_redisTemplate.getHashValues(userId);
        System.out.println("在service计算的key对应的购物车数量为"+hashValues.size());
        if(hashValues.size()>0){
            return hashValues.size();
        }
        return 0;
    }

    /**
     * 获取购物车信息列表展示
     * @param userByToken
     * @return
     */
    @Override
    public List<OmsCartDto> cartList(Long userByToken) {
        Map<Object, Object> hashValues = pet_service_redisTemplate.getHashValues(userByToken);
        //拿到map所有的key
        Set<Object> objects = hashValues.keySet();
        List<OmsCartDto> list=new ArrayList<>();
        for (Object object : objects) {
            //获取到每个购物对象
            OmsCartDto  omsCartDto= (OmsCartDto) hashValues.get(object);
            list.add(omsCartDto);
        }
        return list;
    }
    /**
     * 通过前台传来的用户要删除的商品的id 也许是数组 所以传入的是数组
     */

}
